#http://g.drcnet.com.cn/u/-9993/
from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
import json
import requests
import time
from bs4 import BeautifulSoup
import time
import re
import json
import urllib
from urllib.parse import urlencode

#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):
    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")

    titles = soup.select(".title")
    subtitles = soup.select(".subtitle")
    descs = soup.select(".desc")

    for i in range(len(titles)):
        tempData = resultData()

        try:
            tempData["titleName"] = titles[i].select("a")[0].text
        except:
            pass
        try:
            tempData["titleHref"] = titles[i].select("a")[0]["href"]
        except:
            pass


        try:
            tempData["keyword"] = [subtitles[i].select("u")[0].text]
        except:
            pass
        try:
            tempData["author"] = [subtitles[i].select("font")[0].text]
        except:
            pass
        try:
            tempData["abstract"] = descs[i].text
        except:
            pass

        allData.append(tempData)

    return allData

def getData(keyword):
    # keyword = urllib.parse.quote(keyword)
    # keyword = keyword.encode('gb2312')
    # keyword = urllib.parse.quote(keyword)
    params = {
        "rootlm":"20553^20554^20586^20587^20788^20789^20790^20791^20792^20793^20794^20795^20796^20797^20798^20799^20800^20801^20802^20803^20840^20841^20842^20843^20844^20845^20846^20847^20848^20866^20867^20974^21867^22353^22356^22357^22358^22359^22360^22361^22362^22363^22364^22365^22366^22367^22368^22369^22370^22376^22377^22378^22379^22380^22381^22382^22383^22384^22385^22386^23983^23988^24050",
        "fields":"1^1^1^{}".format(keyword),
    }

    params_gb2312 = urlencode(params, encoding='gb2312')

    response = requests.get('http://s1.drcnet.com.cn/search/page.aspx',params=params_gb2312)

    allData = formData(response)

    return allData
    # NB. Original query string below. It seems impossible to parse and
    # reproduce query strings 100% accurately so the one below is given
    # in case the reproduced version is not "correct".
    # response = requests.get('http://qikan.chaoxing.com/searchjour?adv=T%3D%E7%BB%86%E8%83%9E+AND+A%3Dz&aorp=p&size=50', headers=headers, cookies=cookies)


def index(request):

    data = json.loads(request.POST.get('data', '1'))

    allData = getData(data["values"][0])

    return HttpResponse(json.dumps({
        "collection": allData,
        "allNum": 0
    }), content_type="application/json")